package com.gowants.pems.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.dom4j.DocumentException;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 图片转pdf
 * 
 * @author YLW
 *
 */
public class ImgToPdf {
	 public static void main(String[] args) throws Exception {
		 	ImgToPdf bean = new ImgToPdf();
//	        bean.imgToPdf("E:\\test.jpg","E:\\TEST1.pdf");
		 	String filePath="E:\\test.jpg";
		 	File file = new File(filePath);
		 	InputStream intstream = new FileInputStream(file);
		 	bean.imgToPdf(intstream,"E:\\TEST1.pdf");
	    }

	/**
	 * 将图片转换成pdf文件 imgFilePath 需要被转换的img所存放的位置。
	 * 例如imgFilePath="D:\\projectPath\\55555.jpg"; pdfFilePath 转换后的pdf所存放的位置
	 * 例如pdfFilePath="D:\\projectPath\\test.pdf";
	 * 
	 * @param image
	 * @return
	 * @throws IOException
	 */
	public boolean imgToPdf(String imgFilePath, String pdfFilePath) throws IOException, DocumentException {
		File file = new File(imgFilePath);
		if (file.exists()) {
			Document document = new Document();
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(pdfFilePath);
				try {
					PdfWriter.getInstance(document, fos);
				} catch (com.itextpdf.text.DocumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				// 添加PDF文档的某些信息，比如作者，主题等等
//				document.addAuthor("gwdz");
				document.addSubject("test pdf.");
				// 设置文档的大小
				document.setPageSize(PageSize.A4);
				// 打开文档
				document.open();
				// 写入一段文字
				// document.add(new Paragraph("JUST TEST ..."));
				// 读取一个图片
				Image image;
				try {
					image = Image.getInstance(imgFilePath);
					float imageHeight = image.getScaledHeight();
					float imageWidth = image.getScaledWidth();
					int i = 0;
					while (imageHeight > 500 || imageWidth > 500) {
						image.scalePercent(100 - i);
						i++;
						imageHeight = image.getScaledHeight();
						imageWidth = image.getScaledWidth();
						System.out.println("imageHeight->" + imageHeight);
						System.out.println("imageWidth->" + imageWidth);
					}
					image.setAlignment(Image.ALIGN_CENTER);
					//设置图片的绝对位置
					// image.setAbsolutePosition(0, 0);
					// image.scaleAbsolute(500, 400);
					// 插入一个图片
					try {
						document.add(image);
					} catch (com.itextpdf.text.DocumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (BadElementException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException ioe) {
				System.out.println(ioe.getMessage());
			}
			document.close();
			fos.flush();
			fos.close();
			return true;
		} else {
			return false;
		}
	}
	
	public boolean imgToPdf(InputStream imgInput, String pdfFilePath) throws IOException, DocumentException {
			Document document = new Document();
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(pdfFilePath);
				try {
					PdfWriter.getInstance(document, fos);
				} catch (com.itextpdf.text.DocumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				// 添加PDF文档的某些信息，比如作者，主题等等
//				document.addAuthor("gwdz");
				document.addSubject("test pdf.");
				// 设置文档的大小
				document.setPageSize(PageSize.A4);
				// 打开文档
				document.open();
				// 写入一段文字
				// document.add(new Paragraph("JUST TEST ..."));
				// 读取一个图片
				Image image;
				try {
					image = Image.getInstance(toByteArray(imgInput));
					float imageHeight = image.getScaledHeight();
					float imageWidth = image.getScaledWidth();
					int i = 0;
					while (imageHeight > 500 || imageWidth > 500) {
						image.scalePercent(100 - i);
						i++;
						imageHeight = image.getScaledHeight();
						imageWidth = image.getScaledWidth();
						System.out.println("imageHeight->" + imageHeight);
						System.out.println("imageWidth->" + imageWidth);
					}
					image.setAlignment(Image.ALIGN_CENTER);
					//设置图片的绝对位置
					// image.setAbsolutePosition(0, 0);
					// image.scaleAbsolute(500, 400);
					// 插入一个图片
					try {
						document.add(image);
					} catch (com.itextpdf.text.DocumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (BadElementException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException ioe) {
				System.out.println(ioe.getMessage());
			}
			document.close();
			fos.flush();
			fos.close();
			return true;
		
	}
	public static byte[] toByteArray(InputStream input) throws IOException {
	    ByteArrayOutputStream output = new ByteArrayOutputStream();
	    byte[] buffer = new byte[1024*4];
	    int n = 0;
	    while (-1 != (n = input.read(buffer))) {
	        output.write(buffer, 0, n);
	    }
	    return output.toByteArray();
	}
}
